<?php

namespace app\controller\index\user;

use app\common\lib\TencentCos;
use app\controller\index\BaseController;
use app\exception\IndexException;
use app\logic\index\user\UserLogic;
use support\Request;
use support\Response;

class IndexController extends BaseController
{
    public function getInfo (Request $request): Response
    {
        $result = UserLogic::getInstance()->getInfo();
        return $this->endSuccess($result);
    }

    public function logout(Request $request)
    {
        $data = UserLogic::getInstance()->outLogin();

        return $this->endSuccess($data);
    }

    public function updateUserInfo(Request $request): Response
    {
        $param = [];
        $param['nickname'] = $request->input('nickname');
        $param['headimgurl'] = $request->input('headimgurl');
        $param['scene'] = $request->input('scene');
        $data = UserLogic::getInstance()->updateUserInfo($param);

        return $this->endSuccess($data);
    }

    public function deleteUser(Request $request): Response
    {
        $data = UserLogic::getInstance()->deleteUser();

        return $this->endSuccess($data);
    }

    public function getUserTeamTotal(Request $request): Response
    {
        $param = [];
        $data = UserLogic::getInstance()->getUserTeamTotal($param);
        return $this->endSuccess($data);
    }

    public function getUserTeamList(Request $request): Response
    {
        $param = [];
        $param['keyword'] = $request->input('keyword');
        $param['order'] = $request->input('order');
        $param['level'] = $request->input('level_type');
        $data = UserLogic::getInstance()->getUserTeamList($param);
        return $this->endSuccess($data);
    }

    public function getUserTeamDetail(Request $request): Response
    {
        $param = [];
        $param['user_sn'] = $request->input('user_sn');
        if(empty($param['user_sn'])){
            throw new IndexException(4020,'用户不存在');
        }
        $data = UserLogic::getInstance()->getUserTeamDetail($param);
        return $this->endSuccess($data);
    }

    /**
     * 消费扣点数
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function consumePointCheck(Request $request): Response
    {
        $param = [];
        $param['novel_sn'] = $request->input('novel_sn');
        $param['consume_point'] = $request->input('consume_point');
        if(!empty($param['novel_sn']) && !empty($param['consume_point'])){
            throw new IndexException(4020,'小说编号和消费点数两者传其一');
        }

        $data = UserLogic::getInstance()->consumePointCheck($param);
        return $this->endSuccess($data);
    }

    public function getUserWorksList(Request $request): Response
    {
        $param = [];
        $param['novel_sn'] = $request->input('novel_sn');
        $param['plan_type'] = $request->input('plan_type');
        $param['is_example'] = $request->input('is_example');
        $data = UserLogic::getInstance()->getUserWorksList($param);
        return $this->endSuccess($data);
    }

    public function getUserWorksDetail(Request $request): Response
    {
        $param = [];
        $param['user_planid'] = $request->input('user_planid');
        if(empty($param['user_planid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $data = UserLogic::getInstance()->getUserWorksDetail($param);
        return $this->endSuccess($data);
    }

    /**
     * 作品详情-海外推广
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function getUserWorksOverseaDetail(Request $request): Response
    {
        $param = [];
        $param['user_planid'] = $request->input('user_planid');
        if(empty($param['user_planid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $data = UserLogic::getInstance()->getUserWorksOverseaDetail($param);
        return $this->endSuccess($data);
    }

    public function deleteUserPlan(Request $request):Response
    {
        $param = [];
        $param['user_planid'] = $request->input('user_planid');
        if(empty($param['user_planid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $data = UserLogic::getInstance()->deleteUserPlan($param);

        return $this->endSuccess($data);
    }

    public function updateUserPlanVideo(Request $request):Response
    {
        $param = [];
        $param['user_planid'] = $request->input('user_planid');
        if(empty($param['user_planid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $param['file'] = $request->file('file','');
        $param['path'] = $request->input('path','');

        if($request->file('file')==false){
            throw new IndexException(4020,'请上传文件');
        }
        if($request->file('file') && $request->file('file')->isValid()==false){
            throw new IndexException(4020,'上传失败');
        }

        $param['ext'] = $request->file('file')->getUploadExtension();
        if(in_array(strtolower($param['ext']),['mp4','avi','mov','wav'])==false){
            throw new IndexException('请上传视频');
        }

        $data = UserLogic::getInstance()->updateUserPlanVideo($param);

        return $this->endSuccess($data);
    }

    /**
     * 消费扣点数
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function userConsumePoint(Request $request): Response
    {
        $param = [];
        $param['tool_id'] = $request->input('tool_id');
        $param['consume_point'] = $request->input('consume_point');
        $param['consume_remark'] = $request->input('consume_remark');
        if(!empty($param['tool_id']) && !empty($param['consume_point'])){
            throw new IndexException(4020,'工具ID和消费点数两者传其一');
        }

        $data = UserLogic::getInstance()->userConsumePoint($param);
        return $this->endSuccess($data);
    }

    public function getUserExampleList(Request $request): Response
    {
        $param = [];
        $param['novel_sn'] = $request->input('novel_sn');
        $data = UserLogic::getInstance()->getUserExampleList($param);
        return $this->endSuccess($data);
    }

}
